package com.speljohan.rsbot.script.wrappers;

import java.awt.Point;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.script.Calculations;

public class RSCharacter {

	protected com.speljohan.rsbot.accessors.Character c;

	public RSCharacter(final com.speljohan.rsbot.accessors.Character c) {
		this.c = c;
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof RSCharacter) {
			final RSCharacter cha = (RSCharacter) obj;
			return cha.c == c;
		}
		return false;
	}

	public int getAnimation() {
		return c.getAnimation();
	}

	public int getHeight() {
		return c.getHeight();
	}

	/**
	 * Returns the % of HP. Returns 100 if not in combat.
	 * */
	public int getHPPercent() {
		return isInCombat() ? c.getHPRatio() * 100 / 255 : 100;
	}

	public RSCharacter getInteracting() {
		final int interact = c.getInteracting();
		if (interact == -1) {
			return null;
		}
		if (interact < 32767) {
			return new RSNPC(Bot.getClient().getNPCArray()[interact]);
		} else if (interact >= 32767) {
			int index = interact - 32767;
			if (index == Bot.getClient().getSelfInteracting()) {
				index = 2047;
			}
			return new RSPlayer(Bot.getClient().getPlayerArray()[index]);
		}
		return null;
	}

	public RSTile getLocation() {
		if (c == null) {
			return new RSTile(-1, -1);
		}
		final int x = Bot.getClient().getBaseX() + (c.getX() >> 7);
		final int y = Bot.getClient().getBaseY() + (c.getY() >> 7);
		return new RSTile(x, y);
	}

	public String getMessage() {
		return c.getMessage();
	}

	/**
	 * Get's the minimap location, of the character. Note: This does work when
	 * it's walking!
	 * 
	 * @return
	 */
	public Point getMinimapLocation() {
		final int cX = Bot.getClient().getBaseX() + (c.getX() / 32 - 2) / 4;
		final int cY = Bot.getClient().getBaseY() + (c.getY() / 32 - 2) / 4;
		return Calculations.worldToMinimap(cX, cY);
	}

	public Point getScreenLocation() {
		// RSTile loc = getLocation();
		// return Calculations.tileToScreen(loc.getX(), loc.getY(),
		// 0.5, 0.9, (c.getHeight() / 2));
		return Calculations.worldToScreen(c.getX(), c.getY(),
				-(c.getHeight() / 2));
	}

	public int getTurnDirection() {
		return c.getTurnDirection();
	}

	@Override
	public int hashCode() {
		return System.identityHashCode(c);
	}

	public boolean isInCombat() {
		return Bot.getClient().getLoopCycle() < c.getLoopCycleStatus();
	}

	public boolean isInteractingWithLocalPlayer() {
		return c.getInteracting() - 32768 == Bot.getClient()
		.getSelfInteracting();
	}

	public boolean isMoving() {
		return c.isMoving() != 0;
	}

	public boolean isValid() {
		return c != null;
	}

	@Override
	public String toString() {
		final RSCharacter inter = getInteracting();
		return "[anim="
		+ getAnimation()
		+ ",msg="
		+ getMessage()
		+ ",interact="
		+ (inter == null ? "null" : inter.isValid() ? inter
				.getMessage() : "Invalid") + "]";
	}
}
